//
//  ReadingDetailViewController.m
//  SunXi
//
//  Created by dllo on 15/11/26.
//  Copyright © 2015年 大远. All rights reserved.
//

#import "ReadingDetailViewController.h"

@interface ReadingDetailViewController ()

@property(nonatomic, retain)NSMutableArray *arr;

@property(nonatomic, retain)UITableView *table;

@property (nonatomic, strong) UISegmentedControl *segment;

@property (nonatomic, assign) NSInteger newsLoadTimes;

@property (nonatomic, assign) NSInteger hotLoadTimes;

@property(nonatomic, retain)UIButton *buttonNew;

@property(nonatomic, retain)UIButton *buttonHot;

@property(nonatomic, retain)UIView *viewNew;

@property(nonatomic, retain)UIView *viewHot;

@end

@implementation ReadingDetailViewController


- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self create];
    [self clickNew:self.buttonNew];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma mark - 创建页面框架
- (void)create{
    
    self.table = [[UITableView alloc] init];
    [self.view addSubview:self.table];
    self.table.rowHeight = 150;
    self.table.delegate = self;
    self.table.dataSource = self;
    self.table.separatorStyle = NO;
    
    self.buttonHot = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:self.buttonHot];
    [self.buttonHot setTitle:@"最热" forState:UIControlStateNormal];
    [self.buttonHot addTarget:self action:@selector(clickHot:) forControlEvents:UIControlEventTouchUpInside];
    self.buttonHot.tintColor = [UIColor grayColor];
    
    
    self.buttonNew = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:self.buttonNew];
    [self.buttonNew setTitle:@"最新" forState:UIControlStateNormal];
    [self.buttonNew addTarget:self action:@selector(clickNew:) forControlEvents:UIControlEventTouchUpInside];
    self.buttonNew.tintColor = BUTTONCOLOR;
    
    //    self.buttonNew.layer.borderWidth = 1;
    //    self.buttonHot.layer.borderWidth = 1;
    //    self.table.layer.borderWidth = 1;
    
    self.buttonNew.backgroundColor = [UIColor whiteColor];
    self.buttonHot.backgroundColor = [UIColor whiteColor];
    
    [self.buttonNew mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(SV).with.offset(0);
        make.left.equalTo(SV).with.offset(0);
        make.right.equalTo(self.buttonHot.mas_left).with.offset(0);
        make.bottom.equalTo(self.table.mas_top).with. offset(0);
        make.width.equalTo(@(150));
        make.height.equalTo(@(40));
    }];
    
    [self.buttonHot mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(SV).with.offset(0);
        make.left.equalTo(self.buttonNew.mas_right).with.offset(0);
        make.right.equalTo(SV).with.offset(0);
        make.bottom.equalTo(self.table.mas_top).with. offset(0);
        make.width.equalTo(self.buttonNew);
        make.height.equalTo(self.buttonNew);
    }];
    
    [self.table mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.buttonNew.mas_bottom).with.offset(0);
        make.left.equalTo(SV).with.offset(0);
        make.right.equalTo(SV).with.offset(0);
        make.bottom.equalTo(SV).with. offset(0);
    }];
    
}





#pragma mark - 创建 tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arr.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *reuse = @"reuse";
    ReadingDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[ReadingDetailCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    self.table.rowHeight = 150;
//    cell.model = self.arr[indexPath.row];
    cell.backgroundColor = [UIColor colorWithWhite:0.6 alpha:0.1];
    ReadingDetailModel *model = self.arr[indexPath.row];
    cell.labelTitle.text = model.title;
    cell.image.text = model.content;
    [cell.labelDetail sd_setImageWithURL:[NSURL URLWithString:model.coverimg]placeholderImage:[UIImage imageNamed:@"zan"]];
    return cell;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  
    ReadingDetailModel *model = self.arr[indexPath.row];
    [self createWebData:model];
}




#pragma mark - 给 button 添加点击方法

//      最新按钮
- (void)clickNew:(UIButton *)button{
    
    if (button.tintColor == [UIColor grayColor]) {
        
        button.tintColor = BUTTONCOLOR;
        self.buttonHot.tintColor = [UIColor grayColor];
        [self.viewHot removeFromSuperview];
        
        self.viewNew = [[UIView alloc] initWithFrame:CGRectMake(0, self.buttonNew.frame.size.height - 2, self.buttonNew.frame.size.width, 2)];
        self.viewNew.backgroundColor = BUTTONCOLOR;
        [self.buttonNew addSubview:self.viewNew];
    }
    
    self.arr = [NSMutableArray array];
    [ShowMytext showHUDloadView:SV];
    [self createData];
    self.table.mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        [self.table.mj_header beginRefreshing];
        [self createData];
    }];
    self.table.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [ShowMytext showHUDloadView:SV];
        [self createHotDataNew];
        self.newsLoadTimes++;
    }];
}


//      最热按钮
- (void)clickHot:(UIButton *)button{
    
    if (button.tintColor == [UIColor grayColor]) {
        
        button.tintColor = BUTTONCOLOR;
        self.buttonNew.tintColor = [UIColor grayColor];
        [self.viewNew removeFromSuperview];
        
        self.viewHot = [[UIView alloc] initWithFrame:CGRectMake(0, self.buttonNew.frame.size.height - 2, self.buttonNew.frame.size.width, 2)];
        self.viewHot.backgroundColor = BUTTONCOLOR;
        [self.buttonHot addSubview:self.viewHot];
    }
    
    self.arr = [NSMutableArray array];
    [ShowMytext showHUDloadView:SV];
    [self createHotData];
    self.table.mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        [self.table.mj_header beginRefreshing];
        [self createHotData];
    }];
    self.table.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [ShowMytext showHUDloadView:SV];
        [self createDataNew];
        self.hotLoadTimes++;
    }];
    
    
}




#pragma mark - 数据解析

//      加载最新页面
- (void)createData{
    NSString *newsURL = @"http://api2.pianke.me/read/columns_detail";
    NSDictionary *newsPara = @{@"auth":@"", @"client":@"1", @"deviceid":@"55887AC1-1871-4EAF-BC9F-648F8A10EE3D", @"limit":@"10", @"sort":@"addtime", @"start":@"0", @"typeid":self.model.type, @"version":@"3.0.6"};
    [DyAFnetWorking POSTAfnNetworkingWithUrl:newsURL WithParameters:newsPara Block:^(id result) {
        
        self.arr = [NSMutableArray array];
        self.arr = [ReadingDetailModel baseModelByArr:result[@"data"][@"list"]];
        [self.table reloadData];
        [self.table.mj_header endRefreshing];
        [self.table.mj_footer endRefreshing];
        [ShowMytext hiddHUDloadView];
    }];
}


//      加载下拉刷新
- (void)createDataNew{
    
    NSString *newURL = @"http://api2.pianke.me/read/columns_detail";
    NSDictionary *newPara =  @{@"auth":@"", @"client":@"1", @"deviceid":@"55887AC1-1871-4EAF-BC9F-648F8A10EE3D", @"limit":@"10", @"sort":@"hot", @"start":[NSString stringWithFormat:@"%ld", (self.hotLoadTimes + 1) * 10], @"typeid":self.model.type, @"version":@"3.0.6"};
    [DyAFnetWorking POSTAfnNetworkingWithUrl:newURL WithParameters:newPara Block:^(id result) {
        
        [self.arr addObjectsFromArray:[ReadingDetailModel baseModelByArr:result[@"data"][@"list"]]];
        [self.table reloadData];
        [self.table.mj_header endRefreshing];
        [self.table.mj_footer endRefreshing];
        [ShowMytext hiddHUDloadView];
    }];
}



//      加载最热页面
- (void)createHotData{
    NSString *hotURL = @"http://api2.pianke.me/read/columns_detail";
    NSDictionary *hotPara = @{@"auth":@"", @"client":@"1", @"deviceid":@"55887AC1-1871-4EAF-BC9F-648F8A10EE3D", @"limit":@"10", @"sort":@"hot", @"start":@"0", @"typeid":self.model.type, @"version":@"3.0.6"};
    [DyAFnetWorking POSTAfnNetworkingWithUrl:hotURL WithParameters:hotPara Block:^(id result) {
        
        self.arr = [NSMutableArray array];
        self.arr = [ReadingDetailModel baseModelByArr:result[@"data"][@"list"]];
        [self.table reloadData];
        [self.table.mj_header endRefreshing];
        [self.table.mj_header endRefreshing];
        [ShowMytext hiddHUDloadView];
    }];
}


//      最热页面下拉刷新
- (void)createHotDataNew{
    
    NSString *newsURL = @"http://api2.pianke.me/read/columns_detail";
    NSDictionary *newsPara = @{@"auth":@"", @"client":@"1", @"deviceid":@"55887AC1-1871-4EAF-BC9F-648F8A10EE3D", @"limit":@"10", @"sort":@"addtime", @"start":[NSString stringWithFormat:@"%ld", (self.newsLoadTimes + 1) * 10], @"typeid":self.model.type, @"version":@"3.0.6"};
    [DyAFnetWorking POSTAfnNetworkingWithUrl:newsURL WithParameters:newsPara Block:^(id result) {
        
        [self.arr addObjectsFromArray:[ReadingDetailModel baseModelByArr:result[@"data"][@"list"]]];
        [self.table reloadData];
        [self.table.mj_header endRefreshing];
        [self.table.mj_footer endRefreshing];
        [ShowMytext hiddHUDloadView];
    }];
}



//      点击跳到 webView 的方法
- (void)createWebData:(ReadingDetailModel *)model{
    
    [ShowMytext showHUDloadView:SV];
    NSString *thirdReadingPageUrl = @"http://api2.pianke.me/article/info";
    NSDictionary *para = @{@"auth":@"", @"client":@"1", @"contentid":[NSString stringWithFormat:@"%@", model.Id], @"deviceid":@"55887AC1-1871-4EAF-BC9F-648F8A10EE3D",@"version":@"3.0.6"};
    [DyAFnetWorking POSTAfnNetworkingWithUrl:thirdReadingPageUrl WithParameters:para Block:^(id result) {
        
        NSString *str = result[@"data"][@"shareinfo"][@"url"];
        WebView *webView = [[WebView alloc]init];
        webView.tittle = model.title;
        webView.url = str;
        [self.navigationController pushViewController:webView animated:YES];
        [ShowMytext hiddHUDloadView];
    }];
}




//      重写返回键的方法
- (IBAction)presentLeftMenuViewController:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}




@end









